//
//  AddFamilyViewController.m
//  Cunli
//
//  Created by 湛亚 on 15/6/30.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "AddFamilyViewController.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "UIColor+Addition.h"

@interface AddFamilyViewController (){
    
    float screenWidth;
    float screenHeight;
    UITextField *phoneText,*nameText;
    NSString *areaids,*userid,*usersession;
}

@end

@implementation AddFamilyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithHexString:UIBgColorValue];
    NSDictionary *user=[[NSUserDefaults standardUserDefaults]objectForKey:@"userinfo"];
    areaids=[user objectForKey:@"areaids"];
    userid=[user objectForKey:@"userid"];
    usersession=[user objectForKey:@"usersession"];
    
    self.title=@"亲人邀请";
    screenWidth=[UIScreen mainScreen].bounds.size.width;
    screenHeight=[UIScreen mainScreen].bounds.size.height;
    
    UIImage *textbgImage = [UIImage imageNamed:@"01.png"];
    
    textbgImage = [textbgImage stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    
    phoneText=[[UITextField alloc]initWithFrame:CGRectMake(20, 20, screenWidth-40, 40)];
    phoneText.keyboardType=UIKeyboardTypeNumberPad;
    phoneText.placeholder=@"手机号码";
    [phoneText setBackground:textbgImage];
    [self.view addSubview:phoneText];
    
    nameText=[[UITextField alloc]initWithFrame:CGRectMake(20, 70, screenWidth-40, 40)];
    nameText.placeholder=@"真实姓名";
    [nameText setBackground:textbgImage];
    [self.view addSubview:nameText];
    
    UILabel *remindLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, 120, screenWidth-40, 60)];
    remindLabel.text=@"邀请亲人加入到本村，了解本村的风土人情和精神面貌。";
    //remindLabel.backgroundColor=[UIColor yellowColor];
    remindLabel.font=[UIFont systemFontOfSize:15];
    remindLabel.textAlignment=0;
    remindLabel.numberOfLines=0;
    [self.view addSubview:remindLabel];
    
    UIButton *yaoQingBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    yaoQingBtn.frame=CGRectMake(20,190, screenWidth-40, 35);
    yaoQingBtn.backgroundColor=[UIColor convertHexColorToUIColor:0xc93336];
    yaoQingBtn.layer.cornerRadius =3.0;
    [yaoQingBtn addTarget:self action:@selector(yaoQingClick) forControlEvents:UIControlEventTouchUpInside];
    [yaoQingBtn setTitle:@"马上邀请" forState:UIControlStateNormal];
    [self.view addSubview:yaoQingBtn];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
}

-(void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)yaoQingClick{
    
    if ([phoneText.text isEqualToString:@""]||[nameText.text isEqualToString:@""]) {
        [self showAlertWithMessage:@"输入内容不能为空"];
    }else if ([self isMobileNumberClassification:phoneText.text]==NO){
        [self showAlertWithMessage:@"手机号码无效"];
    }else{
        [self sendYaoQingHttpRequest];
    }
}

-(void)sendYaoQingHttpRequest{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:userid forKey:@"userid"];
    [CheckParams setObject:phoneText.text forKey:@"username"];
    [CheckParams setObject:nameText.text forKey:@"truename"];
    [CheckParams setObject:areaids forKey:@"areaids"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:usersession];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"add_homepeople.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        
        if ([[result objectForKey:@"response"] isEqualToString:@"add_homepeople"]) {
            [self showAlertWithMessage:@"邀请成功!"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            NSString *str=[[result objectForKey:@"error"] objectForKey:@"text"];
            [self showAlertWithMessage:str];
            [self.view endEditing:YES];
            phoneText.text=@"";
            nameText.text=@"";
            //[self.navigationController popViewControllerAnimated:YES];
        }
        
    }failureAction:^{
        
        
    }];
}

- (void)showAlertWithMessage:(NSString *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(BOOL)isMobileNumberClassification:(NSString *)phoneNumber{
    
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    
    if (([regextestmobile evaluateWithObject:phoneNumber] == YES)
        || ([regextestcm evaluateWithObject:phoneNumber] == YES)
        || ([regextestcu evaluateWithObject:phoneNumber] == YES))
    {
        return YES;
    }else
    {
        return NO;
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
